package com.itqf.dao.impl;

import com.itqf.dao.TypeDao;
import com.itqf.entity.Type;
import com.itqf.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TypeDaoImpl implements TypeDao {

    //调用Apache工具类
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Type> selectAll() throws SQLException {
        //编写sql语句
        String sql = "select t_id as tid,t_name as tname ,t_info as tinfo from type limit 5;";
        List<Type> list = queryRunner.query(DbUtils.getConnection(),sql, new BeanListHandler<Type>(Type.class));
        return list;
    }

    @Override
    public List<Type> queryAll() throws SQLException {
        String sql ="select t_id as tid,t_name as tname ,t_info as tinfo ,t_level as tlevel , t_parentName as tparentName from type";

        List<Type> typeList = queryRunner.query(DbUtils.getConnection(), sql, new BeanListHandler<Type>(Type.class));

        return typeList ;


    }

    @Override
    public void addType(Type type) throws SQLException {
        String sql = "insert into type(t_name,t_info,t_level,t_parentName)\n" +
                "values(?,?,?,?)";
        Object[] obj = {type.getTname(),type.getTinfo(),type.getTlevel(),type.getTparentName()};
        queryRunner.update(DbUtils.getConnection(),sql,obj);
    }

    @Override
    public Type selectTypeByTid(String tid) throws SQLException {

        String sql = "";

        Type query = queryRunner.query(DbUtils.getConnection(), sql, new BeanHandler<Type>(Type.class), tid);

        return query;
    }
}
